Assignment 9

Lorenzo Biasi, Julius Vernie


In [1]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
%matplotlib inline

def sigmoid(x):
    return 1. / (1 + np.exp(-x))

def df(x, w=0, theta=0):
    return w * sigmoid(x) * (1 - sigmoid(x))

def f(x, w=0, theta=0):
    return w * sigmoid(x) + theta

def g(x, w, theta):
    return f(x, w, theta) - x

1.1

We can see in the plot the fixed points. In the central point the function has a slope bigger than one, so it is not a stable point. For the other two is the opposite.


In [2]:
x = np.linspace(-5, 5, 100)
theta = -3.5
w = 8.
plt.plot(x, f(x, w, theta))
plt.plot(x, x)
plt.xlabel('x')

FP = []
for x_0 in range(-4, 4):
    temp, _, ier, _ =  fsolve(g, (x_0), args=(w, theta), full_output=True)
    if ier == 1:
        FP.append(temp)
FP = np.array(FP)
plt.plot(FP, f(FP, w, theta), '*')


Out[2]:
[<matplotlib.lines.Line2D at 0x7f0fd8bb0198>]

1.2

We can see from the plot that the fixed points follow an s. The red part is unstable and it corrisponds to the central point of the previous plot. We can also see that when the function "f(x) = x" is tangent to the function there the degeneracy of two fixed point into one. That happens at around $\theta = -3, -5$.


In [3]:
for theta in np.arange(-10, 0, .05):
    x = np.arange(-10, 10, dtype='float')
    stable = []
    unstable = []
    for i in range(len(x)):
        temp, _, ier, _ =  fsolve(g, (x[i]), args=(w, theta), full_output=True)


        if ier == 1:
            x[i] = temp
            if abs(df(temp, w,theta)) < 1:
                stable.append(temp)
            else:
                unstable.append(*temp)
        else:
            x[i] = None
    plt.plot(theta * np.ones(len(stable)), stable, '.', color='green')
    plt.plot(theta * np.ones(len(unstable)), unstable, '.', color='red')


2.1

The fixed point for the function is the logarithm of $r$ and 0.


In [4]:
def l(x, r):
    return r * x * np.exp(-x)

In [5]:
y = np.linspace(-1, 4, 100)

plt.plot(y, l(y, np.exp(3)))
plt.plot(y, y)


Out[5]:
[<matplotlib.lines.Line2D at 0x7f0fa9212f28>]

2.2

In the next plot we can see the dynamics of the function with different values of r. We can see that for low r the points are stable, then they become more oscillatory, and later on they are just chaotic. This can be better seen looking at the biforcation plot.


In [6]:
N = 40
x = np.arange(1, N + 1) * 0.01
for r in np.exp(np.array([1, 1.5, 2, 2.3, 2.7, 3, 3.2, 4.])):
    for i in range(1, N):
        x[i] = l(x[i - 1], r)
    plt.figure()
    plt.plot(x)
    plt.xlabel('x')
    plt.title('log(r) = ' + str(np.log(r)))



In [7]:
Npre = 200
Nplot = 100
x =  np.zeros((Nplot, 1))
for r in np.arange(np.exp(0), np.exp(4), .5):
    x[0] = np.random.random() * 100
    for n in range(Npre):
        x[0] = l(x[0], r)
    for n in range(Nplot - 1):
        x[n + 1] = l(x[n], r)
        
    plt.plot(r * np.ones((Nplot,  1)), x, '.')



In [ ]: